<?php

namespace App\Library\Logic;

use App\Library\Requests\Withdraw\WithdrawPageRequest;
use App\Models\WithdrawModel;

class WithdrawLogic extends WithdrawModel
{

    public function paging(WithdrawPageRequest $request): array
    {
        $builder = self::query()->where('user_id', USERID);
        if (!is_null($request->startAt) && is_null($request->endAt)) $builder->where('created_at', '>=', now()->parse($request->startAt)->startOfDay());
        else if (is_null($request->startAt) && !is_null($request->endAt)) $builder->where('created_at', '<=', now()->parse($request->endAt)->endOfDay());
        else $builder->whereBetween('created_at', [
            now()->parse($request->startAt)->startOfDay(),
            now()->parse($request->endAt)->endOfDay()
        ]);
        return paginate($builder->orderByDesc('created_at')->paginate($request->limit));
    }

    public function auditingMoney()
    {
        return self::query()->where([
            'user_id' => USERID,
            'status' => 0
        ])->sum('amount');
    }
}
